package com.syni.mdd.yhd.common.all.controller.protect;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckParams;
import com.syni.mdd.sjb.common.component.utils.common.annotations.EntranceCheck;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.yhd.common.all.dto.requestDto.groupBuy.DxGroupBuyReDto;
import com.syni.mdd.yhd.common.all.dto.requestDto.groupBuy.GroupBuyUseReDto;
import com.syni.mdd.yhd.common.all.entity.BmsDxGroupBuy;
import com.syni.mdd.yhd.common.all.service.BmsDxGroupBuyService;
import com.syni.mdd.yhd.common.all.validationGroup.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @className 团购
 * @description TOO
 * @Author cfx
 * @DATE 2019/7/24 16:50
 * @VERSION 1.0
 **/
@RestController
@RequestMapping("/dxGroupBuy")
public class BmsDxGroupBuyController {

    @Autowired
    BmsDxGroupBuyService bmsDxGroupBuyService;

    /**
     * @Description 创建团购
     * @Author: cfx
     * @Date: 2019/7/25
     */
    @PostMapping("/createGroupBuy")
    @EntranceCheck(checkAuth = true)
    public Map<String,Object> createGroupBuy(@RequestBody @Validated({GroupInsert.class}) DxGroupBuyReDto dxGroupBuyReDto){
        return bmsDxGroupBuyService.businessCreateGroupBuy(dxGroupBuyReDto);
    }

    /**
     * @Description 查询团购
     * @Author: cfx
     * @Date: 2019/7/25
     */
    @PostMapping("/findGroupBuy")
    @EntranceCheck(checkAuth = true)
    public Map<String,Object> findGroupBuy(@RequestBody @Validated({GroupFind.class, GroupCheckPaging.class}) DxGroupBuyReDto dxGroupBuyReDto){
        return bmsDxGroupBuyService.businessFindGroupBuy(dxGroupBuyReDto);
    }

    /**
     * @Description 更新团购券
     * @Author: cfx
     * @Date: 2019/7/25
     */
    @PostMapping("/updateGroupBuy")
    @EntranceCheck(checkAuth = true)
    public Map<String,Object> updateGroupBuy(@RequestBody @Validated({GroupUpdate.class}) DxGroupBuyReDto dxGroupBuyReDto){
        return bmsDxGroupBuyService.businessUpdateGroupBuy(dxGroupBuyReDto);
    }


    /**
     * @Description 更新团购券状态
     * @Author: cfx
     * @Date: 2019/7/25
     */
    @PostMapping("/updateGroupBuyStatus")
    @EntranceCheck(checkAuth = true)
    public Map<String,Object> updateGroupBuyStatus(@RequestBody @Validated({GroupUpdate.class}) DxGroupBuyReDto dxGroupBuyReDto){
        return bmsDxGroupBuyService.businessUpdateGroupBuyStatus(dxGroupBuyReDto);
    }

    /**
     * @Description 删除团购券
     * @Author: cfx
     * @Date: 2019/7/25
     */
    @PostMapping("/deleteGroupBuy")
    @EntranceCheck(checkAuth = true)
    public Map<String,Object> deleteGroupBuy(@RequestBody @Validated({GroupDelete.class}) DxGroupBuyReDto dxGroupBuyReDto){
        return bmsDxGroupBuyService.businessDeleteGroupBuy(dxGroupBuyReDto);
    }

    /**
     * @Description 用户端获取商家的团购券
     * @Author: cfx
     * @Date: 2019/7/25
     */
    @PostMapping("/userGetBusinessGroupBuy")
    public Map<String,Object> userGetBusinessGroupBuy(@RequestBody ParamsMap paramsMap) throws Exception {
        return bmsDxGroupBuyService.userGetBusinessGroupBuy(paramsMap);
    }

    /**
     * @Description 商户端核销团购券
     * @Author: cfx
     * @Date: 2019/7/25
     */
    @PostMapping("/businessUserVerGroupBuy")
    @EntranceCheck(checkAuth = true)
    public Map<String,Object> businessUserVerGroupBuy(@RequestBody @Validated({GroupInsert.class}) GroupBuyUseReDto groupBuyUseReDto) throws Exception {
        return bmsDxGroupBuyService.businessUserVerGroupBuy(groupBuyUseReDto);

    }

    /**
     * @Description 商户端统一扫码核销接口
     * @Author: cfx
     * @Date: 2019/7/25
     */
    @PostMapping("/businessUserVerGroupBuyByScan")
    @EntranceCheck(checkAuth = true)
    public Map<String,Object> businessUserVerGroupBuyByScan(@RequestBody GroupBuyUseReDto groupBuyUseReDto) throws Exception {
        return bmsDxGroupBuyService.businessUserVerGroupBuyByScan(groupBuyUseReDto);
    }

    /**
     * @Description 商户端核销团购券（券码核销）
     * @Author: cfx
     * @Date: 2019/7/25
     */
    @PostMapping("/businessUserVerGroupBuyByCode")
    @EntranceCheck(checkAuth = true)
    public Map<String,Object> businessUserVerGroupBuyByCode(@RequestBody @Validated({GroupInsert.class}) GroupBuyUseReDto groupBuyUseReDto) throws Exception {
        return bmsDxGroupBuyService.businessUserVerGroupBuyByCode(groupBuyUseReDto);
    }

    @PostMapping("/recordGroupBuyVisitNum")
    @EntranceCheck
    public Map<String,Object> recordGroupBuyVisitNum(@RequestBody DxGroupBuyReDto dxGroupBuyReDto) throws Exception {
        return bmsDxGroupBuyService.recordGroupBuyVisitNum(dxGroupBuyReDto);
    }


    @PostMapping("/getGroupBuyPreferential")
    @CheckParams
    public Map<String,Object> getGroupBuyPreferential(@RequestBody ParamsMap paramsMap) {
        return bmsDxGroupBuyService.getGroupBuyPreferential(paramsMap);
    }

    @PostMapping("/getGroupBuyPreferentialMore")
    @CheckParams
    public Map<String,Object> getGroupBuyPreferentialMore(@RequestBody ParamsMap paramsMap) {
        return bmsDxGroupBuyService.getGroupBuyPreferentialMore(paramsMap);
    }

    @PostMapping("/getGroupBuyDetail")
    @CheckParams
    public Map<String,Object> getGroupBuyDetail(@RequestBody ParamsMap paramsMap) {
        return bmsDxGroupBuyService.getGroupBuyDetail(paramsMap);
    }


    @PostMapping("/getGroupBuyMore")
    @CheckParams
    public Map<String,Object> getGroupBuyMore(@RequestBody ParamsMap paramsMap) {
        return bmsDxGroupBuyService.getGroupBuyMore(paramsMap);
    }


    /** 
     * @Description: 限时团购
     * @Author: cfx 
     * @Date: 2019/11/13 
     */
    @PostMapping("/getLimitBuyGroupBuy")
    @CheckParams
    public Map<String,Object> getLimitBuyGroupBuy(@RequestBody ParamsMap paramsMap) {
        return bmsDxGroupBuyService.getLimitBuyGroupBuy(paramsMap);
    }

    /**
     * @Description: 更多团购
     * @Author: cfx
     * @Date: 2019/11/13
     */
    @PostMapping("/getAllGroupBuy")
    @CheckParams
    public Map<String,Object> getAllGroupBuy(@RequestBody ParamsMap paramsMap) {
        Map<String,Object> retMap = bmsDxGroupBuyService.getAllGroupBuy_v2(paramsMap);
        if(retMap.containsKey("data")){
            List<BmsDxGroupBuy> bmsDxGroupBuys = (List<BmsDxGroupBuy>) retMap.get("data");
            if(bmsDxGroupBuys.size() == 0){
                if(paramsMap.getString("addrCity") != null && !paramsMap.getString("addrCity").equals("珠海市")) {
                    paramsMap.remove("addrCity");
                    paramsMap.remove("addrDistrict");
                    return bmsDxGroupBuyService.getAllGroupBuy_v2(paramsMap);
                }
            }
        }
        return retMap;
    }

    /**
     * @Description: 获取更多列表，用于点击更多使用
     * @Author: cfx
     * @Date: 2019/11/13
     */
    @PostMapping("/getBusinessMoreGroupBuy")
    @CheckParams
    public Map<String,Object> getBusinessMoreGroupBuy(@RequestBody ParamsMap paramsMap) {
        return bmsDxGroupBuyService.getBusinessMoreGroupBuy(paramsMap);
    }

    /**
     * @Description: 商户获取团购详情
     * @Author: cfx
     * @Date: 2019/11/13
     */
    @PostMapping("/getMerchantGroupBuyDetail")
    @CheckParams
    public Map<String,Object> getMerchantGroupBuyDetail(@RequestBody ParamsMap paramsMap) {
        return bmsDxGroupBuyService.getMerchantGroupBuyDetail(paramsMap);
    }


    public static void main(String[] args) {
        JSONArray jsonArray = JSON.parseArray("[{\"libraryId\":21,\"choose\":\"2_1\"}]");
        System.out.println(jsonArray);
    }


}
